<?php

/**
 * This is the model class for table "subscription".
 *
 * The followings are the available columns in table 'subscription':
 * @property integer $subscription_id
 * @property integer $business_id
 * @property string $start_date
 * @property string $end_date
 * @property string $transfer_method
 * @property integer $subscription_fee
 * @property string $subscription_type
 *
 * The followings are the available model relations:
 * @property Business $business
 */
class Subscription extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Subscription the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'subscription';
	}

    /**
     * @return override the CActiveRecord::defaultScope method, so that only display user's own record.
     * id used here is the business id stored in account table
     */
    public function defaultScope()
    {
        return array(
            'condition'=>'business_id='.Yii::app()->user->id,
        );
    }

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
        // Not needed for the moment "business_id, start_date, end_date, transfer_method, subscription_fee,"
		return array(
			array('subscription_type', 'required'),
			array('business_id, subscription_fee', 'numerical', 'integerOnly'=>true),
			array('transfer_method, subscription_type', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('subscription_id, business_id, start_date, end_date, transfer_method, subscription_fee, subscription_type', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'business' => array(self::BELONGS_TO, 'Business', 'business_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'subscription_id' => 'Subscription',
			'business_id' => 'Business',
			'start_date' => 'Start Date',
			'end_date' => 'End Date',
			'transfer_method' => 'Transfer Method',
			'subscription_fee' => 'Subscription Fee',
			'subscription_type' => 'Subscription Type',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('subscription_id',$this->subscription_id);
		$criteria->compare('business_id',$this->business_id);
		$criteria->compare('start_date',$this->start_date,true);
		$criteria->compare('end_date',$this->end_date,true);
		$criteria->compare('transfer_method',$this->transfer_method,true);
		$criteria->compare('subscription_fee',$this->subscription_fee);
		$criteria->compare('subscription_type',$this->subscription_type,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}